/*
 * Copyright (C) 2004, Velocityme Partnership
 * StateMachineActions.java
 *
 * Created on 19 October 2004, 06:47
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.entity.TaskStateMachine;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.TaskStateMachineLocal;
import com.velocityme.www.actionforms.StateMachineActionForm;
import com.velocityme.www.utility.NodeUtil;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class StateMachineActions extends NodeActions {

    /** Creates a new instance of StateMachineActions */
    public StateMachineActions() {
    }

    @Override
    public ActionForward showEdit(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        TaskStateMachine taskStateMachine = lookupTaskStateMachineBean().findTaskStateMachine(key, nodeId);
        session.setAttribute("taskStateMachine", taskStateMachine);
        return mapping.findForward("ACTION_FORWARD");
    }

    public ActionForward showEditForward(ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        TaskStateMachine taskStateMachine = (TaskStateMachine)session.getAttribute("taskStateMachine");
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        StateMachineActionForm stateMachineAF = (StateMachineActionForm)form;
        stateMachineAF.setCmd(0,"");
        stateMachineAF.setTransitions(taskStateMachine.getTransitions());
        session.setAttribute("statePairs", NodeUtil.getAllStatePairs(key));
        session.setAttribute("allButStartStatePairs", NodeUtil.getAllButStartStatePairs(key));
        session.setAttribute("statusPairs", NodeUtil.getAllStatusPairs(key));
        session.setAttribute("rolePairs", NodeUtil.getAllRolePairs(key));
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        TaskStateMachine taskStateMachine = (TaskStateMachine)session.getAttribute("taskStateMachine");
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        StateMachineActionForm stateMachineAF = (StateMachineActionForm)form;
        if (stateMachineAF.processCmd()) {
            stateMachineAF.setCmd(0,"");
            return mapping.findForward("ACTION_EDIT");
        }
        if (request.getParameter("btnSubmit").equals("OK")) {
            taskStateMachine.setTransitions(stateMachineAF.getTransitions(key));
            lookupTaskStateMachineBean().editTaskStateMachine(key, taskStateMachine);
        }
        Long nodeId = taskStateMachine.getId();
        request.setAttribute("nodeId", nodeId);
        return mapping.findForward("ACTION_SUCCESS");
    }

    private TaskStateMachineLocal lookupTaskStateMachineBean() {
        try {
            Context c = new InitialContext();
            return (TaskStateMachineLocal) c.lookup("java:comp/env/TaskStateMachineBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
